$(function() {
    var mp3 = document.getElementById("Mymp3");
    var hammerlogin = $("#loginBox");
    var hammerbiaoti2 = $(".biaoti2");
    var hammerloginAjax = $("#loginAjax");
    var num = localStorage.getItem("colorRunuser");


        //按下鼠标之后
        $(function(){
            $('.ditu').mousedown(function(){
                mp3.play();
                $(this).addClass('add1');
            });
        //松开鼠标之后
            $('.ditu').mouseup(function(){
                $(this).removeClass('add1');
            });
            //随机 函数
            function GetRandomNum(Min,Max){
                var Range = Max - Min;
                var Rand = Math.random();
                return(Min + Math.round(Rand * Range));
            };
            //遍历 出现与消失 位置
            function show(){
                var index = GetRandomNum(0,8);
                $('.box').eq(index).find('img').animate({marginTop:0},200)
                setTimeout(hide,3000);
                function hide(){
                    $('.box').eq(index).find('img').animate({marginTop:'80px'},200)
                };
            };
            //点击（敲打）成功后 计分板获得分数
            $('.box img').click(function(){
                var n = Number($('#valScore').val());
                $(this).animate({marginTop:'80px'},200);
                n++;
                $('#valScore').val(n)
                //得分
                $('.score').html(n);
            });
            hammerlogin.on("click", function () {
                $("#loginBox").hide();
            });

            hammerbiaoti2.on("click", function () {
                $("#loginBox").show();

                $.ajax({
                    type:"post",
                    url:"/gamelist",
                    data:{},
                    success:function(dt){
                        console.log(dt);
                        var html="";
                        for(var i = 0; i<dt.length; i++){
                            html+="<tr> <th scope='row'>"+(i+1)+"</th> " +
                                "<td>"+dt[i].user+"</td> <td>"+dt[i].num+"</td>" +
                                " <td>"+dt[i].day+"</td> </tr>";
                        }
                        $(".list-show").html(html);
                    }
                })
            });

            hammerloginAjax.on("click", function () {
                console.log("1121")
                var user = $('.inputBoxlist > input[type="text"]').val();
                console.log(user);
                if (user == '' || user == null || user == undefined) {
                    return false;
                }
                var name = localStorage.getItem("colorRunuser");
                if (name) {
                    $("#inputBox").hide();
                }else{
                    $.ajax({
                        type:"get",
                        url:"/gamelogin",
                        data:{
                            user:user
                        },
                        success:function(dt){
                            console.log(dt)
                            if(dt.node==1){
                                localStorage.setItem("colorRunuser",dt.user[0].user);
                                $("#inputBox").hide();
                            }
                        }
                    })
                }


            })




            //var ctx = Mycnavas[0].getContext("2d");
            //开始游戏
            $('.start').click(function(){
                var num = localStorage.getItem("colorRunuser");
                if(num){
                    $('.box_win').hide();
                    $('.text').hide();
                    gamestart();
                }else{
                    $("#inputBox").show();
                }

                function gamestart(){

                            var diff = Number($('#selDiff').val());
                            var t = Number($('#selTime').val());
                            var start = setInterval(time,1000);
                            var num = setInterval(show,diff*1000);
                            var n2 = t/diff/3*2;
                            $('.time').html(t);
                            $('#valScore').val('0');
                            $('.score').html('0');
                            var a=Number($('.time').html());
                            function time(){
                                a--;
                                if(a < 0){
                                    clearInterval(start);
                                    clearInterval(num);
                                    var grade = '';
                                    var n = Number($('#valScore').val());
                                    var n3 = t/diff;
                                    var n2 = t/diff/3*2;
                                    var n1 = t/diff/3;
                                    if(n == 0){
                                        grade = '搞个铲铲哦！'
                                    }
                                    if(n>0 && n<n1){
                                        grade = '有点笨！'
                                    }
                                    if(n>=n1 && n<n2){
                                        grade = '不错！'
                                    }
                                    if(n>=n2 && n<n3){
                                        grade = '很棒！'
                                    }
                                    if(n == n3){
                                        grade = '完美！'
                                    }
                                    $('.box_win').show();
                                    $('.win_score').show();
                                    $('.cj').html('GameOver，您一共得了'+n+'分，'+grade);
                                    var n = Number($('#valScore').val());
                                    var user = localStorage.getItem("colorRunuser");
                                    $.ajax({
                                        type:"post",
                                        url:"/gamenum",
                                        data:{
                                            user:user,
                                            num:n
                                        },
                                        success:function(dt){
                                            console.log(dt);
                                        }
                                    })
                                }else{
                                    $('.time').html(a);
                                }
                            };
                }
            });







            $('.again').click(function(){
                $('.start').click();
                $('.win_score').hide();
            });
            $('.close').click(function(){
                window.open("about:blank","_self").close()
            });

        })
});